/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */
package lk.mrt.cse.wihidum.storage;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.apache.log4j.Logger;

public class ConfigurationDAO implements ConfigurationManager {

    Session session = null;
    private static final Logger logger = Logger.getLogger(MessageTraceDAO.class);

    public ConfigurationDAO() {
        this.session = DataBaseManager.getSessionFactory().getCurrentSession();
    }

    public void changeTopology(String topology) {
        try {
            Transaction tx = session.beginTransaction();
            Criteria criteria = session.createCriteria(Configuration.class);
            Configuration configuration = (Configuration) criteria.list().get(0);
            configuration.setTopology(topology);
            session.update(configuration);
            tx.commit();
            logger.info("Topology changed successfully");
        }
        catch (HibernateException ex) {
            ex.printStackTrace();
        }
    }

    public String getTopology() {
        try {
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Configuration.class);
            Configuration configuration = (Configuration) criteria.list().get(0);
            return configuration.getTopology();
        }
        catch (HibernateException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
